//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmSpreadPayoffInnerValue.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpIterator.h>
#include <gen/QL/Instruments/BasketPayoff.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmInnerValueCalculator.h>
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (Cephei::QL::Instruments::IBasketPayoff^ payoff, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calc1, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calc2) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CBasketPayoff^ _Cpayoff;
    CFdmInnerValueCalculator^ _Ccalc1;
    CFdmInnerValueCalculator^ _Ccalc2;
    try
    {
#ifdef HANDLE
        _phFdmSpreadPayoffInnerValue = NULL;
#endif
        _Cpayoff = safe_cast<CBasketPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::BasketPayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::BasketPayoff>&> (_Cpayoff->GetShared ()); 
        _Ccalc1 = safe_cast<CFdmInnerValueCalculator^> (calc1);
        _Ccalc1->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _calc1 = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_Ccalc1->GetShared ()); 
        _Ccalc2 = safe_cast<CFdmInnerValueCalculator^> (calc2);
        _Ccalc2->Lock();
        boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& _calc2 = static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>&> (_Ccalc2->GetShared ()); 
        _ppFdmSpreadPayoffInnerValue = new boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue> (new QuantLib::FdmSpreadPayoffInnerValue ( _payoff,  _calc1,  _calc2 ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Ccalc1 != nullptr) _Ccalc1->Unlock();
        if (_Ccalc2 != nullptr) _Ccalc2->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSpreadPayoffInnerValue = NULL;
#endif
	_ppFdmSpreadPayoffInnerValue = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (QuantLib::FdmSpreadPayoffInnerValue& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmSpreadPayoffInnerValue = NULL;
#endif
	_ppFdmSpreadPayoffInnerValue = new boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue> (&childNative);
    
    _FdmSpreadPayoffInnerValueOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (CFdmSpreadPayoffInnerValue^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSpreadPayoffInnerValue = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmSpreadPayoffInnerValue = new boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSpreadPayoffInnerValue = NULL;
#endif
	if (!t->IsSubclassOf(CFdmSpreadPayoffInnerValue::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (QuantLib::Handle<QuantLib::FdmSpreadPayoffInnerValue>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSpreadPayoffInnerValue = &childNative;
	_ppFdmSpreadPayoffInnerValue = &static_cast<boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue>>(childNative.currentLink());
    
    _FdmSpreadPayoffInnerValueOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (QuantLib::Handle<QuantLib::FdmSpreadPayoffInnerValue> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSpreadPayoffInnerValue = &childNative;
	_ppFdmSpreadPayoffInnerValue = &static_cast<boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::CFdmSpreadPayoffInnerValue (QuantLib::FdmSpreadPayoffInnerValue childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSpreadPayoffInnerValue = NULL;
#endif
	_ppFdmSpreadPayoffInnerValue = new boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue> (new QuantLib::FdmSpreadPayoffInnerValue (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::~CFdmSpreadPayoffInnerValue ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmSpreadPayoffInnerValue != NULL)
    {
	    delete _ppFdmSpreadPayoffInnerValue;
        _ppFdmSpreadPayoffInnerValue = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::!CFdmSpreadPayoffInnerValue ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmSpreadPayoffInnerValue != NULL)
    {
	    delete _ppFdmSpreadPayoffInnerValue;
    }
}
QuantLib::FdmSpreadPayoffInnerValue& Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::GetReference ()
{
    if (_ppFdmSpreadPayoffInnerValue == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmSpreadPayoffInnerValue;
}
boost::shared_ptr<QuantLib::FdmSpreadPayoffInnerValue>& Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::GetShared ()
{
    if (_ppFdmSpreadPayoffInnerValue == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmSpreadPayoffInnerValue;
}
QuantLib::FdmSpreadPayoffInnerValue* Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::GetPointer ()
{
    if (_ppFdmSpreadPayoffInnerValue == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmSpreadPayoffInnerValue;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmSpreadPayoffInnerValue>& Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::GetHandle ()
{
	if (_phFdmSpreadPayoffInnerValue == NULL)
	{
		_phFdmSpreadPayoffInnerValue = new Handle<QuantLib::FdmSpreadPayoffInnerValue> (*_ppFdmSpreadPayoffInnerValue);
	}
	return *_phFdmSpreadPayoffInnerValue;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::HasNative () 
{
	return (_ppFdmSpreadPayoffInnerValue != NULL);
}

Double Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::AvgInnerValue (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, Double t)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmSpreadPayoffInnerValue)->avgInnerValue ( _iter,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
Double Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue::InnerValue (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, Double t)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmSpreadPayoffInnerValue)->innerValue ( _iter,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmSpreadPayoffInnerValue^ Cephei::QL::Experimental::Finitedifferences::CFdmSpreadPayoffInnerValue_Factory::Create (Cephei::QL::Instruments::IBasketPayoff^ payoff, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calc1, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmInnerValueCalculator^ calc2)
{
    return REFNEW CFdmSpreadPayoffInnerValue ( payoff,  calc1,  calc2);
}
